import java.util.*;

public class 组合 {
    /**
     * https://leetcode.cn/problems/combinations/
     * 剪枝加上dfs
     */

    List<List<Integer>> ans;
    List<Integer> path;
    int n,k;
    public List<List<Integer>> combine(int n, int k) {
        this.n = n;
        this.k = k;
        ans = new ArrayList<>();
        path = new ArrayList<>();
        dfs(0,1);
        return ans;
    }
    //n 和 k 是题目给的, pos是层数, start是开始的地方, 前面遍历过了后面就不要进行遍历
    public void dfs(int pos,int start){
        if(pos == k){
            List<Integer> cur = new ArrayList<>(path);
            ans.add(cur);
            return;
        }
        for(int i = start;i <= n;i++){
            path.add(i);
            dfs(pos + 1,i + 1);
            path.remove(path.size() - 1);
        }
    }
}
